﻿using gbL.NetBoard.Helpers;
using gbL.NetBoard.Examples.DoubleMVC.View.UI.Requests;

namespace gbL.NetBoard.Examples.DoubleMVC.View.UI
{
    public class MessageView : TypedMessageListener<MessageRequest>
    {
        public IOutputTarget Output { get; set; }

        public MessageView(Blackboard domain, IOutputTarget output) : base(domain) { Output = output;  }

        public MessageView(Blackboard domain) : this(domain, new ConsoleOutputTarget()) { }

        protected override void OnMessageAdded(MessageRequest message)
        {
            var taken = Messages.Take(e => e.Message == message);
            if (taken.Count == 0) return;
            if (message.BeginsNewLine) Output.WriteLine();
            Output.Write(message.Text);
            if (message.EndsNewLine) Output.WriteLine();
        }

        protected override void OnMessageRemoved(MessageRequest message)
        {
            //
        }
    }
}
